package com.zyj;

import com.zyj.cache.service.DataRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 */
@EnableCaching
@SpringBootApplication
public class App {
//    public static void main(String[] args) throws InterruptedException {
//        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
//        DataRepository dataRepository = context.getBean(DataRepository.class);
//        for (int i = 0; i < 10000; i++) {
//            TimeUnit.MILLISECONDS.sleep(10);
//            dataRepository.getUseLocalCache(i % 5);
//        }
//    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        DataRepository dataRepository = context.getBean(DataRepository.class);
        while (true) {
            TimeUnit.SECONDS.sleep(1);
            for (int i = 0; i < 100; i++) {
                //org.springframework.cache.annotation.Cacheable.sync 这个属性对本地缓存有效
                executorService.submit(() -> dataRepository.getUseLocalCache(1));
                //org.springframework.cache.annotation.Cacheable.sync 对redis缓存有效
                executorService.submit(() -> dataRepository.getUseRedisSync(2));
            }
        }
    }
}
